<?php

include_once dirname(__FILE__) . "/../error.php";

class Api_test extends PHPUnit_Framework_TestCase
{
    const SVR_URL = "http://umealwith.me:8888/v1/api.php";        
    const OPEN_QQ = "/home/david/soft/nginx/www/v1/test/.qq.dat";
    private function send_req($data)
    {
        $data = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::SVR_URL);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
    
    public function test_api_with_invalid_post_data()
    {
        $arg = array();
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue($ret['code'] == ec_post_error);
    }

    public function test_serv_with_invalid_interface()
    {
        $arg = array(
            "interface" => "not_exist_interface",
        );
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue($ret['code'] == ec_inf_error);
    }
    
    public function test_api_get_dish_list_with_new_user()
    {
        global $err_code;
        $req = array(
            "open_id" => "xyz_new", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_info",
        );
        $rsp = array(
            "code" => 0, 
            "msg" => "okkkkkkkkk",
            "nickname" => "test",
            "province" => "广东",
            "city" => "深圳",
        );
        $open_qq = array(
            json_encode($req) => $rsp,
        );
        file_put_contents(self::OPEN_QQ, serialize($open_qq));

        $arg = array(
            "open_id" => "xyz_new", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_dish_list",
        );
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($ret['code'] == ec_ok);
    }
    public function test_api_get_dish_list_with_old_user()
    {
        global $err_code;
        $req = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_info",
        );
        $rsp = array(
            "code" => 0, 
            "msg" => "okkkkkkkkk",
            "nickname" => "test",
            "province" => "广东",
            "city" => "深圳",
        );
        $open_qq = array(
            json_encode($req) => $rsp,
        );
        file_put_contents(self::OPEN_QQ, serialize($open_qq));

        $arg = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_dish_list",
        );
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($ret['code'] == ec_ok);
    }
    
    public function test_get_recommend_accounts_empty()
    {
        $arg = array(
            "open_id" => "xyz_not_exists", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_recommend_accounts",
            "selected" => array(),
            "num" => 1,
        );
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($ret['code'] == ec_ok);
    }
    
    public function test_get_recommend_accounts_ok()
    {
        $arg = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_recommend_accounts",
            "selected" => array(),
            "num" => 1,
        );
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($ret['code'] == ec_ok);
    }
    public function test_get_recommend_restaurants_empty()
    {
        $arg = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_recommend_restaurants",
            "dish_list" => "",
            "num" => 1,
        );
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($ret['code'] == ec_ok);
    }
    
    public function test_get_recommend_restaurants_ok()
    {
        $arg = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_recommend_restaurants",
            "dish_list" => "60", //麻辣烫
            "num" => 2,
        );
        $ret = $this->send_req($arg);
        //print_r($ret);
        $ret = json_decode($ret, true);
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($ret['code'] == ec_ok);
    }
    /*
     */
}
?>
